package com.huiquan.icd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.icd.service.IcdDiseaseArticleService;

@Controller
public class IcdDiseaseArticleController {

	@Autowired
	private IcdDiseaseArticleService icdDiseaseArticleService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/disease_article_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("modifyFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		Integer start = RequestParamUtil.getIntegerDefaultDi(req, "start", 1);
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "codeSearch");
		String diseaseSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "diseaseSearch");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return icdDiseaseArticleService.list(start, codeSearch,  exactSearchFlag,countPerPageStr,diseaseSearch);
	}
	@RequestMapping(value = "/icd/disease_article_edit.htm")
	public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer start = RequestParamUtil.getIntegerDefault1LargeThan1(req, "start");
		String codeSearch = RequestParamUtil.getTrimString(req, "codeSearch");
		String nameSearch = RequestParamUtil.getTrimString(req, "nameSearch");
		String categorySearch = RequestParamUtil.getTrimString(req, "categorySearch");
		String exactSearchFlag = RequestParamUtil.getTrimString(req, "exactSearchFlag");
		String id = req.getParameter("id");
		User user = (User) req.getSession().getAttribute("user");

		return icdDiseaseArticleService.preEdit(codeSearch, nameSearch, categorySearch, start, id, user, exactSearchFlag);
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/icd/update_disease_article.do")
	public ReturnData updateAndAddArticle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String title = RequestParamUtil.getTrimString(req, "title");
		String disease = RequestParamUtil.getTrimString(req, "disease");
		String url = RequestParamUtil.getTrimString(req, "url");
		String id = req.getParameter("id");
		String type = RequestParamUtil.getTrimString(req, "type");
		String source = RequestParamUtil.getTrimString(req, "source");
		String content = RequestParamUtil.getTrimString(req, "content");

		User user = (User) req.getSession().getAttribute("user");

		return icdDiseaseArticleService.updateAndAddDiseaseArticl(id,title,disease,url,type,source,content,user);
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/icd/delete_disease_article.do")
	public ReturnData deleteGaoge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = Long.parseLong(req.getParameter("id"));
		return icdDiseaseArticleService.deleteArticle(id);
	}
	
}
